package com.jic.market.impl.rest.discount.processor;

import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discount.model.ConditionDiscountModel;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import com.jic.market.request.rest.discount.DiscountQueryRequest;
import com.jic.market.response.rest.discount.GiftDetail;
import com.jic.market.service.rest.discount.GiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 满量-换
 * @author yinhaijun
 * @date: 2020/5/7
 */
@Service("ChangeFullNumProcessor")
public class ChangeFullNumProcessor extends AbstractConditionDiscountProcessor {
    @Autowired
    private PromotionRuleEntityMapper promotionRuleEntityMapper;
    @Autowired
    private GiftService giftService;
    @Override
    public ConditionResult isCondition(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request, DiscountAmountServiceOperateEnum operate) {
        if(!checkPromotion(((DiscountBaseRequest)request).getThirdUserId(), ((DiscountBaseRequest)request).getAdminOperate().getChildId().toString(), conditionDiscountModel.getChannelCodes(), conditionDiscountModel.getPromotionMemberGradeList())){
            return new ConditionResult(false, "满量计算"+conditionDiscountModel.getPromotionCode()+"会员等级校验未通过");
        }
        AtomicReference<Long> countValidNum = new AtomicReference<>(0L);
        List<String> thirdSkuIdList = conditionDiscountModel.getThirdSkuIdList();
        thirdSkuIdList.forEach((thirdSkuId)->{
            GoodsDetailModel goodsDetailModel = goodsDetailModelMap.get(thirdSkuId);
            countValidNum.set(countValidNum.get() + goodsDetailModel.getNum());
        });
        // 获取对应匹配的规则
        List<PromotionRuleEntity> promotionRuleEntityList = promotionRuleEntityMapper.queryByPromotionId(Long.parseLong(conditionDiscountModel.getPromotionId()));
        PromotionRuleEntity promotionRuleEntity = getFlightPromotionRuleEntity(new BigDecimal(countValidNum.get()), promotionRuleEntityList, 3);
        // 不优惠处理
        if(null == promotionRuleEntity){
            return new ConditionResult(false, "满量换无优惠");
        }

        // 查询出所有的换购的商品
        List<GiftDetail> giftDetailList = giftService.getGiftDetails(conditionDiscountModel.getPromotionId(), conditionDiscountModel.getPromotionCode(), ((DiscountBaseRequest) request).getThirdUserId(), promotionRuleEntity, 1L);

        if(null == giftDetailList || giftDetailList.isEmpty()){
            return new ConditionResult(false, "无换购商品");
        }
        ConditionResult conditionResult = new ConditionResult(true, "有优惠");
        conditionResult.setObject(giftDetailList);
        return conditionResult;
    }

    @Override
    public void queryShoppingCartAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        List<GiftDetail> giftDetailList = (List<GiftDetail>) conditionResult.getObject();
        conditionDiscountModel.setGiftDetailList(giftDetailList);
    }

    @Override
    public void querySettlementAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        List<GiftDetail> giftDetailList = (List<GiftDetail>) conditionResult.getObject();
        DiscountQueryRequest discountQueryRequest = (DiscountQueryRequest) request;
        Map<String, List<GiftDetail>> giftMap = discountQueryRequest.getGiftMap();
        if(null != giftMap && !giftMap.isEmpty()){
            List<GiftDetail> giftDetailList1 = giftService.getMixedGiftDetails(giftMap.get(conditionDiscountModel.getPromotionCode()), giftDetailList);
            conditionDiscountModel.setGiftDetailList(giftDetailList1);
        }
    }


    @Override
    public void createAfter(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){


    }

    // type 1金额、2折扣、3数量
    protected PromotionRuleEntity getFlightPromotionRuleEntity(BigDecimal condition, List<PromotionRuleEntity> promotionRuleEntityList, Integer type) {
        if(null != promotionRuleEntityList && !promotionRuleEntityList.isEmpty()){
            PromotionRuleEntity promotionRuleEntity = null;
            Integer level = -1;
            for(int i = 0; i<promotionRuleEntityList.size();i++){
                Long conditionValue = promotionRuleEntityList.get(i).getConditionValue();
                BigDecimal conditionValueTrue = null;
                if(type.equals(1)){
                    conditionValueTrue = new BigDecimal(conditionValue/100);
                }
                else if(type.equals(2)){
                    conditionValueTrue = new BigDecimal(conditionValue/10);
                }
                else if(type.equals(3)){
                    conditionValueTrue = new BigDecimal(conditionValue);
                }
                if(promotionRuleEntityList.get(i).getLevel() > level && condition.compareTo(conditionValueTrue) != -1){
                    promotionRuleEntity = promotionRuleEntityList.get(i);
                    level = promotionRuleEntityList.get(i).getLevel();
                }
            }
            return promotionRuleEntity;

        }
        return null;
    }

}
